<?php    
/* 
* @package TDE Newsletter
* @version: 1.0 
* @last modified: 10:28 AM 06/15/2007
* http://www.topdreamweaverextensions.com
*/
  require_once "./../config.php";
  require_once "./language/".ADMIN_LANGUAGE.".inc.php";
  
  
  define ('MSG_ADDED', 'Successfully subscribed. Thank you!');
  define ('MSG_DELETED', 'Successfully removed. Thank you.');
  define ('MSG_NOTFOUND', 'The email entered has not been found in the list.');
  define ('MSG_ALREADY', 'Email address already subscribed. Thank you!');

  if ($_GET["task"] == 'subscribe')
  {     
      $rows = 0;
      $lastitem = 0;
      $buffer = "";
      if (file_exists(DATABASE_SUBSCRIBERS))
      {
          $fp = fopen (DATABASE_SUBSCRIBERS,"r");
          while ($data = fgetcsv ($fp, 1000, ",")) 
          {
              $rows++;
              if (strtolower($data[1]) == strtolower($_GET["email"]))
              {
              	 echo MSG_ALREADY;
              	 exit;
              }
              $buffer.= '"'.str_replace('"','\"',$data[0]).'","'.str_replace('"','\"',$data[1]).'","'.str_replace('"','\"',$data[2]).'","'.str_replace('"','\"',$data[3])."\"\n";
              $lastitem = $data[0];    
          }
          fclose ($fp); 
      }
      $buffer.= '"'.($lastitem+1).'","'.str_replace('"','\"',$_GET["email"]).'","'.date("Y-m-d H:i:s").'","'.str_replace('"','\"',$_GET["name"])."\"\n";
          
      $fp = fopen (DATABASE_SUBSCRIBERS,"w");
      fwrite($fp, $buffer);
      fclose ($fp);                
      
	    echo MSG_ADDED;
	    exit;     
  }
  else if ($_GET["task"] == 'unsubscribe')
  {
      $file = '';
      $rows = 0;
      $notfound = true;
      $fp = fopen (DATABASE_SUBSCRIBERS,"r");
      while ($data = fgetcsv ($fp, 1000, ",")) 
      {
          $rows++;
          if (strtolower($data[1]) != strtolower($_GET["email"]))
              $file .= '"'.str_replace('"','\"',$data[0]).'","'.str_replace('"','\"',$data[1]).'","'.str_replace('"','\"',$data[2]).'","'.str_replace('"','\"',$data[3])."\"\n";
          else 
             $notfound = false;    
      }
      fclose ($fp);      
      
      $fp = fopen (DATABASE_SUBSCRIBERS,"w");
      fwrite($fp, $file);
      fclose ($fp);      
      
      if ($notfound)
          echo MSG_NOTFOUND;
      else
          echo MSG_DELETED;
	    exit;    	    	
  }

?>  